﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basicas;
using ProjetoJornada_Negocios.Fachada;

namespace ProjetoJornada.Interno
{
    public partial class Matricula_ListarPorCpf : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Jornada"] == null)
                {
                    Response.Redirect("Jornada_Escolher.aspx?urlReturn=" + HttpContext.Current.Request.Url.AbsolutePath);
                }

                string queryString = Request.QueryString["cpf"];
                if (queryString != null)
                {
                    this.CPFTextBox.Text = queryString;
                    this.ListarMatriculas();
                }
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            this.ListarMatriculas();   
        }

        protected void GridView_Matricula_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<Matricula> matriculas = (List<Matricula>)Session["Matriculas"];
            Matricula matricula = matriculas[e.RowIndex];
            Fachada.Instance.RemoverMatricula(matricula);
            matriculas.Remove(matricula);

            this.GridView_Matricula.DataSource = matriculas;
            this.GridView_Matricula.DataBind();
        }

        protected void GridView_Matricula_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            bool teste = true;
            teste = !teste;

            if (e.Row.DataItem is Matricula)
            {
                Matricula matricula = e.Row.DataItem as Matricula;

                Label nome = e.Row.FindControl("lblNome") as Label;
                nome.Text = matricula.Aluno.Nome;

                Label data = e.Row.FindControl("lblData") as Label;
                data.Text = matricula.DataMatricula.ToString("dd/MM/yyyy");

                Label status = e.Row.FindControl("lblStatus") as Label;
                if (matricula.StatusPagamento)
                {
                    status.Text = "Pago";
                }
                else
                {
                    status.Text = "Em aberto.";
                }

                Label curso = e.Row.FindControl("lblCurso") as Label;
                curso.Text = matricula.Turma.Curso.Nome;
            }
        }

        private void ListarMatriculas()
        {
            String cpf = this.CPFTextBox.Text;
            cpf = cpf.Replace(".", "");
            cpf = cpf.Replace("-", "");
            Int32 idJornada = 0;

            if (Session["Jornada"] != null)
            {
                idJornada = Convert.ToInt32(Session["Jornada"]);
            }

            List<Matricula> matriculas = Fachada.Instance.ConsultarMatriculaPorCPFNaJornada(cpf, idJornada);
            if (matriculas.Count != 0)
            {
                Session["Matriculas"] = matriculas;
                this.GridView_Matricula.DataSource = matriculas;
                this.GridView_Matricula.DataBind();
            }
            else
            {
                this.lblMensagem.Text = String.Format("Nao foram encontradas matriculas para o aluno de CPF {0}", this.CPFTextBox.Text);
            }
        }
    }
}